﻿using System;
using System.Xml;
using SdmxMl.Helper;

namespace SdmxMl.Common
{
    /// <summary>
    /// AnnotationType provides for non-documentation notes and annotations
    /// to be embedded in data and structure messages. It provides optional
    /// fields for providing a title, a type description, a URI, and the text
    /// of the annotation.
    /// </summary>
    public class Annotation
    {
        #region Constructor

        public Annotation()
        {
            Init();
        }

        public Annotation(string type)
        {
            Init();
            Type = type;
        }
        public Annotation(XmlNode nodeArtefact, XmlNamespaceManager nsmgr)
        {
            Init();

            Type = Xmlhelper.GetNodeText(nodeArtefact, "./common:AnnotationType", nsmgr);
            Title = Xmlhelper.GetNodeText(nodeArtefact, "./common:AnnotationTitle", nsmgr);
            string uri = Xmlhelper.GetNodeText(nodeArtefact, "./common:AnnotationURL", nsmgr);
            if (UrlValidator.IsValidUrl(uri))
                Url = new Uri(uri);

            MultiLangNodeReader mr = new MultiLangNodeReader();
            mr.Read(Text, nodeArtefact, "AnnotationText", "common", nsmgr, false);
        }

        public Annotation Clone()
        {
            Annotation an = new Annotation(Type);
            an.Text = Text.Clone();
            an.Title = Title;
            an.Url = Url == null ? null : new Uri(Url.ToString());

            return an;
        }

        private void Init()
        {
            Text = new MultiLang();
        }
        #endregion

        #region Properties

        /// <summary> This is a language-specific string which holds the text oif the annotation.</summary>
        public MultiLang Text { get; set; }


        /// <summary> AnnotationTitle provides a title for the annotation.</summary>
        public string Title { get; set; }

        /// <summary>
        /// AnnotationType is used to distinguish between annotations designed to support various uses. 
        /// The types are not enumerated, as these can be specified by the user or creator of the annotations. 
        /// The definitions and use of annotation types should be documented by their creator.
        /// </summary>
        public string Type { get; set; }

        /// <summary>
        /// This is a URI - typically a URL - which points to an external resource which may contain or supplement the annotation. If a specific behavior is desired, an annotation type should be defined which specifies the use of this field more exactly.This is a URI - typically a URL - which points to an external resource which may contain or supplement the annotation. If a specific behavior is desired, an annotation type should be defined which specifies the use of this field more exactly.        This is a URI - typically a URL - which points to an external resource which may contain or supplement the annotation. If a specific behavior is desired, an annotation type should be defined which specifies the use of this field more exactly.
        /// </summary>
        public Uri Url { get; set; }

        #region Compare

        public bool SameAs(Annotation a)
        {
            bool same = false;
            if (a != null)
            {
                same = a.Type == Type &&
                    a.Title == Title &&
                    a.Text.SameAs(Text) &&
                    a.Url == Url;
            }

            return same;
        }



        #endregion

        #region SDMX 2.1

        /// <summary>
        /// The id attribute provides a non-standard identification of an annotation. It can be used to disambiguate annotations.
        /// </summary>
        public string Id { get; set; }

        #endregion

        #endregion
    }

   
}
